<?php

namespace App\Store\Models\Wxapp;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;

class WxappCoupon extends Model
{
    protected $table = 'site_wxapp_coupon';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    /**
     *获取列表 
     */
    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::where(['cp_id' => $cp_id,'is_del'=>1])
        ->orderBy('create_at', 'desc');

        $stock_type=$request->stock_type;
        if($stock_type!='' && in_array($stock_type,['NORMAL','DISCOUNT'])){
            $query->where('stock_type',$stock_type);
        }

        $stock_name=$request->stock_name;
        if ($stock_name != '') {
            $query->where('stock_name', 'like', '%' . $stock_name . '%');
        }


        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    /**
     *获取列表 
     */
    public static function getListByMem(Request $request,$cp_id)
    {
        $query = self::where(['cp_id' => $cp_id,'is_del'=>1])->where([['available_begin_time','<=',time()],['available_end_time','>=',time()]])
        ->select(['id','store_ids','img_path','stock_name','goods_name','stock_type','discount_amount','discount_percent','transaction_minimum','max_coupons_per_user','coupon_code_count'])->orderBy('create_at', 'desc');
        $limit = $request->limit ?? 10;
        $list = $query->paginate($limit)->toArray();
        return Common::paginateDataToArray($list);
    }
}
